﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Bet_Yatziv
{
    public partial class Rooms : Form
    {
        //init
        public Rooms()
        {
            InitializeComponent();
            refresh();
        }

        //returns to menu
        private void button6_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //loading table
        private void button5_Click(object sender, EventArgs e)
        {
            DataTable table = DataAccessLayer.GetAllRooms();
            dataGridView1.DataSource = table;
        }

        //clearing
        private void button3_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource = null;
            clearAll();
            disableText();
            unshowOK();
            mainAdd.Enabled = true;
            mainDelete.Enabled = true;
        }

        //adding a new room
    private void Add()
    {
        try
        {
            if (txt_roomNum.Text == "")
                MessageBox.Show("אנא הכנס את מספר החדר ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
            else
                if (txt_dailyPrice.Text == "")
                    MessageBox.Show("אנא הכנס מחיר יומי לחדר ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);   
                    else
                        if (txt_roomType.Text == "")
                            MessageBox.Show("אנא הכנס את סוג החדר ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        else
                            if (txt_roomCapacity.Text == "")
                                MessageBox.Show("אנא הכנס את כמות המיטות המקסימאלית לחדר ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                        else
                                        {
                                    string room = txt_roomNum.Text;
                                    string price = txt_dailyPrice.Text;
                                    Boolean available = ck_available.Checked;
                                    string capacity = txt_roomCapacity.Text;
                                    string roomType = txt_roomType.Text;

                                    int roomNum = 0;
                                    int priceNum = 0;
                                    int bedNum = 0;

                                    //converting capacity
                                    if (capacity.Length > 0)
                                    {

                                        try
                                        {
                                            bedNum = Convert.ToInt32(capacity);
                                        }
                                        catch (FormatException exp)
                                        {
                                            MessageBox.Show(exp.Message + "מספר מיטות חייב להיות ערך מספרי", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                            txt_roomCapacity.Text = "";
                                            return;
                                        }
                                    }

                                    //converting room number
                                    if (room.Length > 0)
                                    {
                                        try
                                        {
                                            roomNum = Convert.ToInt32(room);
                                        }
                                        catch (FormatException exp)
                                        {
                                            MessageBox.Show(exp.Message + "לא ניתן להמיר ערך של מספר חדר", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                            txt_roomNum.Text = "";
                                            return;
                                        }
                                    }
                                    //converting price
                                    if (price.Length > 0)
                                    {
                                        try
                                        {
                                            priceNum = Convert.ToInt32(price);
                                        }
                                        catch (FormatException exp)
                                        {
                                            MessageBox.Show(exp.Message + "לא ניתן להמיר ערך של מחיר", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                            txt_dailyPrice.Text = "";
                                            return;
                                        }
                                    }
                                    DataAccessLayer.AddNewRoom(roomNum,roomType,priceNum,bedNum,available);
                                    MessageBox.Show("החדר התווסף לסניף בהצלחה", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                    refresh();
                                }
            }
            catch
            {
                MessageBox.Show("החדר כבר קיים במערכת", "הודעה");
            }
        }

        //refreshing
        private void refresh()
        {
            dataGridView1.DataSource = null;
            DataTable dt = DataAccessLayer.GetAllRooms();
            dataGridView1.DataSource = dt;
            clearAll();

            DataTable table = DataAccessLayer.GetAllRooms();
            string query1 = "select roomID from tbl_rooms";
            DataTable dt1 = AdoHelper.ExecuteDataTable(query1);

            txt_roomNum.Items.Clear();
            txt_roomType.Items.Clear();

            for (int i = 0; i < dt1.Rows.Count; i++)
                txt_roomNum.Items.Add(dt.Rows[i].ItemArray[0]);

            txt_roomType.Items.Add("Regular");
            txt_roomType.Items.Add("Premium");
            txt_roomType.Items.Add("Suite");
        }

        //deleting a room
        private void Delete()
        {
            if (txt_roomNum.Text == "")
                MessageBox.Show("אנא הכנס מספר חדר", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
            else
            {
                string room = txt_roomNum.Text;
                int roomNumber = 0;
                if (room.Length > 0)
                {
                    try
                    {
                        roomNumber = Convert.ToInt32(room);
                    }
                    catch (FormatException exp)
                    {
                        MessageBox.Show(exp.Message + "לא ניתן להמיר פרמטר", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        txt_roomNum.Text = "";
                        return;
                    }

                    int i = 0;
                    string query1 = "select * from tbl_rooms";
                    DataTable dt1 = AdoHelper.ExecuteDataTable(query1);
                    for (i = 0; i < dt1.Rows.Count; i++)
                        if (roomNumber == (int)(dt1.Rows[i].ItemArray[0]))
                            break;

                    if (i >= 0 && i < dt1.Rows.Count)
                    {
                        if (MessageBox.Show("מחיקת החדר תמחק את כל המידע הרלוונטי אליו במערכת, האם אתה בטוח שברצונך למחוק את החדר", "מחיקת חדר", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                        {
                            DataAccessLayer.DeleteRoom(roomNumber);
                            MessageBox.Show("החדר נמחק בהצלחה", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            refresh();
                        }
                    }
                    else
                    {
                        MessageBox.Show("החדר לא קיים", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }

     

        //checking which rooms are available
        private void button6_Click_1(object sender, EventArgs e)
        {
            string query = "Select * from tbl_rooms where (((tbl_rooms.available)=True));";
            DataTable dt = AdoHelper.ExecuteDataTable(query);
            dataGridView1.DataSource = dt;
        }
        
        //cancel button
        private void cancel_Click(object sender, EventArgs e)
        {
            disableText();
            unshowOK();;
            mainDelete.Enabled = true;
            clearAll();
        }

        //main add button
        private void mainAdd_Click(object sender, EventArgs e)
        {
            showOK();
            enableText();

            mainDelete.Enabled = true;
            mainAdd.Enabled = false;
            clearAll();
        }

        //main delete button
        private void mainDelete_Click(object sender, EventArgs e)
        {
            showOK();
            disableText();
            mainDelete.Enabled = false;
            mainAdd.Enabled = true;
        }

        //main update button
        private void mainUpdate_Click(object sender, EventArgs e)
        {
            showOK();
            enableText();
            mainDelete.Enabled = true;
            mainAdd.Enabled = true;
        }

        //adding the room details from the combobox license
        private void txt_license_SelectedIndexChanged(object sender, EventArgs e)
        {
            int i = 0;
            string query1 = "select * from tbl_rooms";
            DataTable dt1 = AdoHelper.ExecuteDataTable(query1);

            int licNum = 0;
            if (txt_roomNum.Text.Length > 0)

                try
                {
                    licNum = Convert.ToInt32(txt_roomNum.Text);
                }
                catch (FormatException exp)
                {
                    MessageBox.Show(exp.Message + "מספר החדר חייב להיות ערך מספרי", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txt_roomNum.Text = "";
                    return;
                }

            for (i = 0; i < dt1.Rows.Count; i++)
                if (licNum == (int)(dt1.Rows[i].ItemArray[0]))
                    break;

            txt_dailyPrice.Text = "" + dt1.Rows[i].ItemArray[2];
            ck_available.Checked = (Boolean)(dt1.Rows[i].ItemArray[4]);
            txt_roomCapacity.Text = "" + dt1.Rows[i].ItemArray[3];
            txt_roomType.Text = "" + dt1.Rows[i].ItemArray[1];
        }

        //clearing all text
        public void clearAll()
        {
            txt_roomNum.Text = txt_dailyPrice.Text = "";
            txt_roomType.Text = txt_roomCapacity.Text = "";
            ck_available.Checked = false;
        }

        //enabling ok buttons
        public void showOK()
        {
            ok.Visible = true;
            cancel.Visible = true;
        }

        //disabling ok buttons
        public void unshowOK()
        {
            ok.Visible = false;
            cancel.Visible = false;
        }

        //enabling all textboxes
        public void enableText()
        {
            txt_dailyPrice.Enabled = true;
            txt_roomType.Enabled = true;
            txt_roomCapacity.Enabled = true;
            ck_available.Enabled = true;
        }

        //disabling all textboxes
        public void disableText()
        {
            txt_dailyPrice.Enabled = false;
            txt_roomType.Enabled = false;
            txt_roomCapacity.Enabled = false;
            ck_available.Enabled = false;
        }

        //ok button
        private void ok_Click(object sender, EventArgs e)
        {
            if (mainDelete.Enabled == false)
                Delete();
            if (mainAdd.Enabled == false)
                Add();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Hide();
            roomStatus rs = new roomStatus();
            rs.ShowDialog();
            this.Show();
        }
    }
}